$(function(){
    //加载数据
    loadDatas();

    //初始化日期控件
    $('#begin_time').datetimepicker({
        format: 'yyyy-mm-dd hh:ii:00',
        language: 'zh-CN',
        autoclose: true,
        minView: "hour",
        minuteStep: 5,
        startDate: new Date()
    }).on("changeDate",function(ev){
        $('#end_time').datetimepicker('setStartDate', ev.date);
    });
    $('#end_time').datetimepicker({
        format:'yyyy-mm-dd hh:ii:00',
        language: 'zh-CN',
        autoclose: true,
        minView: "hour",
        minuteStep: 5,
        startDate: new Date()
    }).on("changeDate",function(ev){
        $('#begin_time').datetimepicker('setEndDate', ev.date);
    });
});


//数据加载
function loadDatas(){

    $.ajax({
        type: "post",
        url: "../../../php/tonggao/loadTongGaoListDatas.php",
        beforeSend: function(){
            $("#load").css("display","block");
        },
        success: function(msg){
            $("#load").css("display","none");
          var   msg=eval('(' + msg + ')');
            var datas = msg.records;
            var html = "";
            //表格数据
            if(datas.length==0){
                html += "<tr><td colspan='9'>没查询到符合条件的数据~</td></tr>";
            }else{
                for(var i = 0; i<datas.length; i++){
                    html += "<tr>";
                    html += "<td>"+datas[i].id+"</td>";
                    html += "<td width='350px'>";
                    html += "<div class='hidenConent' style='width:300px;' title='"+datas[i].remark+"' onclick='updateTongGao("+datas[i].id+")'>";
                    html += datas[i].remark;
                    html += "</div>";
                    html += "</td>";
                    html += "<td>"+datas[i].sort+"</td>";
                    // html += "<td style='font-size:16px;'>";
                    // if(datas[i].state=='1'){
                    //     html+="已发送";
                    // }else{
                    //     html+="未发送";
                    // }
                    // html += "<span class='fa fa-pencil-square-o' style='cursor: pointer; color:#daa40b;' title='修改状态' onclick='TongGao("+datas[i].id+")'></span>";
                    // html += "</td>";
                    html += "<td style='font-size:16px;'>";
                    html += "<span class='fa fa-pencil-square-o' style='cursor: pointer; color:#daa40b;' title='修改' onclick='updateTongGao("+datas[i].id+")'></span>";
                    html += "<span class='fa fa-trash' style='margin-left:10px; cursor: pointer; color:#dd5044' title='删除' onclick='deleteTongGao("+datas[i].id+")'></span>";
                    // if(datas[i].state=='1'){
                    //     html +="<span></span>";
                    // }else{
                    //     html +="<span style='cursor: pointer;margin-left: 20px;' title='状态' onclick='change_("+datas[i].id+")'>改状态</span>";
                    // }
                    html += "</td>";
                    html += "</tr>";
                }
            }
            $("#databody").html(html);
        },
        error: function(msg){
            alert("连接出错！");
        }
    });
}
function change_() {
    $("#delId1").val(id);
    $("#confirmWindow1").modal("show");
}
//加载要修改的数据
function updateTongGao(id){
    $.ajax({
        type: "post",
        data: {"id": id},
        url: "../../../php/tonggao/loadTongGaoById.php",
        beforeSend: function(){
            $("#load").css("display","block");
        },
        success: function(msg){
            $("#load").css("display","none");
            var   msg=eval('(' + msg + ')');
            var gongGaoObj = msg.records[0];
            $("#id").val(gongGaoObj.id);
            // $("#ggTitle").val(gongGaoObj.title);
            $("#ggContent").val(gongGaoObj.remark);
            $("#sortNo").val(gongGaoObj.sort);
            var on = gongGaoObj.isOnline;
            $("#isOnline option[value="+on+"]").attr("selected","selected");

            $("#editGongGaoWindow").modal("show");
        }
    });
}
//删除公告确定框
function deleteTongGao(id){
    $("#delId").val(id);
    $("#confirmWindow").modal("show");
}
//提交删除
function subDel(){
    var id = $("#delId").val();
    $.ajax({
        type: "post",
        data: {"id": id},
        url: "../../../php/tonggao/delTongGaoById.php",
        beforeSend: function(){
            cannelDel();//初始化确定框
            $("#load").css("display","block");
        },
        success: function(msg){
            $("#load").css("display","none");
            if(msg == "1"){
                loadDatas();//重新加载数据
            }else{
                alert(msg);
            }
        }
    });
}
//提交删除
function subDel1(){
    var id = $("#delId").val();
    $.ajax({
        type: "post",
        data: {"id": id},
        url: "../../../php/tonggao/changeTongGaoById.php",
        beforeSend: function(){
            cannelDel();//初始化确定框
            $("#load").css("display","block");
        },
        success: function(msg){
            $("#load").css("display","none");
            if(msg == "1"){
                loadDatas();//重新加载数据
            }else{
                alert(msg);
            }
        }
    });
}
//取消删除
function cannelDel(){
    $("#delId").val("");
    $("#confirmWindow").modal("hide");
}
function cannelDel1(){
    $("#delId1").val("");
    $("#confirmWindow1").modal("hide");
}
//提交公告编辑
function subEditTongGao(){
    var id = $("#id").val();//id
    var content = $.trim($("#ggContent").val());//内容
    var sortNo = $("#sortNo").val();//排序序号
    //判断内容
    if(content == ""){
        $("#ggContent").css("border","1px solid #dd5044");
        $("#content_error").css("display","block");
    }else{
        $("#ggContent").css("border","");
        $("#content_error").css("display","none");
    }
    //排序序号
    if(sortNo == ""){
        $("#sortNo").css("border","1px solid #dd5044");
        $("#sort_error").css("display","block");
    }else{
        $("#sortNo").css("border","");
        $("#sort_error").css("display","none");
    }
    if(content != "" && sortNo != ""){
        var editUrl = "";
        if(id == ""){//id为空时，添加公告
            editUrl = "../../../php/tonggao/addTongGao.php";
        }else{//id不为空时，编辑公告
            editUrl = "../../../php/tonggao/updateTongGao.php";
        }

//		alert(editUrl);
        if(editUrl != ""){
            $.ajax({
                type: "post",
                data: {
                    "id": id,

                    "content": content,

                    "sortNo": sortNo,

                },
                url: editUrl,
                beforeSend: function(){
                    $("#load").css("display","block");
                },
                success: function(msg){
                    $("#load").css("display","none");
//					console.log(msg);
                    if(msg == "1"){
                        cannelEdit();//初始化编辑框
                        loadDatas();//重新加载数据
                    }else{
                        alert(msg);
                    }
                }
            });
        }else{
            alert("请求错误，请稍后重试！");
        }
    }

}

//取消编辑公告
function cannelEdit(){
    $("#editGongGaoWindow").modal("hide");
    $("#resetGGForm").click();
    $("#id").val("");
    $("#ggTitle, #ggContent, #begin_time, #end_time, #imgUrl, #sortNo, #isOnline").css("border","");
    $(".fa-remove, .fa-exclamation").css("display","none");
}
